package eiisan.function;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author gragonfly
 * @date 2019/8/21
 **/
public class PersonTest {
    public static void main(String[] args) {
        Person person1 = new Person("张三", 18);
        Person person2 = new Person("李四", 12);
        Person person3 = new Person("王五", 39);
        List<Person> persons = Arrays.asList(person1, person2, person3);
        PersonTest personTest = new PersonTest();
        List<Person> personResult = personTest.getPersonsByUsername("王五", persons);
        personResult.forEach(person -> System.out.println(person.getUsername()));
        System.out.println("--------------------");

        List<Person> personResult2 = personTest.getPersonsByAge(13, persons);
        personResult2.forEach(person -> System.out.println(person.getAge()));
        System.out.println("--------------------");

        List<Person> personResult3 = personTest.getPersonsByAge2(19, persons);
        personResult3.forEach(person -> System.out.println(person.getAge()));
        System.out.println("--------------------");

        //BiFunction
        List<Person> personResult4 = personTest.getPersonsByAge3(19, persons, (ageOfPerson, personList) -> personList.stream().filter(person -> person.getAge() > ageOfPerson).collect(Collectors.toList()));
        personResult4.forEach(person -> System.out.println(person.getAge()));
        System.out.println("--------------------");
        //Predicate
        Predicate<String> predicate = str -> str.length() > 5;
        System.out.println(predicate.test("wollollo"));
    }

    public List<Person> getPersonsByUsername(String username, List<Person> persons) {
        //Predicate
        return persons.stream().filter(person -> person.getUsername().equals(username)).collect(Collectors.toList());
    }

    public List<Person> getPersonsByAge(int age, List<Person> persons) {
        //Predicate
        return persons.stream().filter(person -> person.getAge() > age).collect(Collectors.toList());
    }

    public List<Person> getPersonsByAge2(int age, List<Person> persons) {
        //BiFunction
        BiFunction<Integer, List<Person>, List<Person>> biFunction = (ageOfPerson, personList) -> {
            return personList.stream().filter(person -> person.getAge() > ageOfPerson).collect(Collectors.toList());
        };
        return biFunction.apply(age, persons);
    }

    public List<Person> getPersonsByAge3(int age, List<Person> persons, BiFunction<Integer, List<Person>, List<Person>> biFunction) {
        return biFunction.apply(age, persons);
    }
}
